package org.example.demo4.config;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import io.swagger.v3.oas.models.info.Contact;
import org.apache.commons.lang3.StringUtils;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.hutool.core.util.RandomUtil;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;


/**
 * <p> Knife4j配置类 </p>
 *
 * @author zhengqingya
 * @description
 * @date 2022/6/21 14:01
 */
@Configuration
public class Knife4jConfig {

	@Value("${spring-doc.info.title}")
	private String title;
	@Value("${spring-doc.info.desc}")
	private String desc;
	@Value("${spring-doc.info.version}")
	private String version;

	@Value("${spring-doc.info.concat.name}")
	private String uname;
	@Value("${spring-doc.info.concat.url}")
	private String url;
	@Value("${spring-doc.info.concat.email}")
	private String email;

//	@Bean
//	public GroupedOpenApi publicApi() {
//		return GroupedOpenApi.builder()
//				.group(title)
//				.pathsToMatch("/**")
//				.packagesToScan("org.example.demo4.controller.freight")
//				.addOpenApiCustomiser(sortTagsAlphabetically())
//				.build();
//	}
//
//	public GlobalOpenApiCustomizer sortTagsAlphabetically() {
//		return openApi -> openApi.setTags(openApi.getTags()
//				.stream()
//				.sorted(Comparator.comparing(tag -> StringUtils.stripAccents(tag.getName())))
//				.collect(Collectors.toList()));
//	}
//
//	   /* 根据@Tag 上的排序，写入x-order
//	     *
//	     * @return the global open api customizer
//	     */
	    @Bean
	    public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
	        return openApi -> {
	            if (openApi.getTags()!=null){
	                openApi.getTags().forEach(tag -> {
	                    Map<String,Object> map=new HashMap<>();
	                    map.put("x-order", RandomUtil.randomInt(0,100));
	                    tag.setExtensions(map);
	                });
	            }
	            if(openApi.getPaths()!=null){
	                openApi.addExtension("x-test123","333");
	                openApi.getPaths().addExtension("x-abb",RandomUtil.randomInt(1,100));
	            }

	        };
	    }

	    @Bean
	    public OpenAPI customOpenAPI() {
	        return new OpenAPI()
	                .info(new Info()
	                        .title(title)
	                        .version(version)
	                        .description( desc)
	                        .termsOfService(url)
							.contact(new Contact()
									.name(uname)
									.url(url)
									.email(email))
	                        .license(new License().name("Apache 2.0")
	                                .url("http://doc.xiaominfo.com")));
	    }

}
